package com.ruoyi.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.system.domain.SysUserRole;
import com.ruoyi.system.mapper.SysUserRoleMapper;
import com.ruoyi.system.service.ISysUserRoleService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * @author 明一G
 * @date 2023/3/25 16:06
 */
@Service
public class SysUserRoleServiceImpl implements ISysUserRoleService {
	@Resource
	SysUserRoleMapper userRoleMapper;

	@Override
	public int insertByUserIdAndRoleIds(Long userId, Long[] roleIds) {
		if (Objects.isNull(userId)) {
			return -1;
		}
		if (Objects.isNull(roleIds)) {
			return -1;
		}
		try {
			int result = 0;
			for (Long roleId : roleIds) {
				SysUserRole userRole = new SysUserRole();
				userRole.setUserId(userId);
				userRole.setRoleId(roleId);
				result += userRoleMapper.insert(userRole);
			}
			return result;
		}catch (Exception e){
			return -1;
		}
	}

	@Override
	public int insert(SysUserRole userRole) {
		if (Objects.isNull(userRole)) {
			return -1;
		}
		try {
			return userRoleMapper.insert(userRole);
		} catch (Exception e) {
			e.printStackTrace();
			return -1;
		}
	}

	@Override
	public List<SysUserRole> getByUserId(Long userId) {
		if (Objects.isNull(userId)) {
			return null;
		}
		try {
			LambdaQueryWrapper<SysUserRole> queryWrapper = new LambdaQueryWrapper<>(new SysUserRole());
			queryWrapper.eq(SysUserRole::getUserId, userId);
			return userRoleMapper.selectList(queryWrapper);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	@Override
	@Transactional
	public int deleteByUserId(Long userId) {
		if (Objects.isNull(userId)) {
			return -1;
		}
		try {
			LambdaQueryWrapper<SysUserRole> queryWrapper = new LambdaQueryWrapper<>(new SysUserRole());
			queryWrapper.eq(SysUserRole::getUserId, userId);
			return userRoleMapper.delete(queryWrapper);
		} catch (Exception e) {
			e.printStackTrace();
			return -1;
		}
	}

	@Override
	@Transactional
	public int deleteByUserIds(Long[] userIds) {
		if (Objects.isNull(userIds)) {
			return -1;
		}
		try {
			int result = 0;
			for (Long userId : userIds) {
				result += deleteByUserId(userId);
			}
			return result;
		} catch (Exception e) {
			return -1;
		}
	}

	@Override
	public boolean checkUserHasRoleByUserId(Long userId) {
		if (Objects.isNull(userId)) {
			return false;
		}
		try {
			LambdaQueryWrapper<SysUserRole> queryWrapper = new LambdaQueryWrapper<>(new SysUserRole());
			queryWrapper.eq(SysUserRole::getUserId, userId);
			return userRoleMapper.exists(queryWrapper);
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

	@Override
	public Long countUserRoleByRoleId(Long roleId) {
		if(Objects.isNull(roleId)){
			return 0L;
		}
		try {
			LambdaQueryWrapper<SysUserRole> queryWrapper = new LambdaQueryWrapper<>(new SysUserRole());
			queryWrapper.eq(SysUserRole::getRoleId,roleId);
			return userRoleMapper.selectCount(queryWrapper);
		}catch (Exception e){
			e.printStackTrace();
			return 0L;
		}
	}
}
